<template>
	<view class="content">
		<u-navbar title="我的订单" :placeholder='true' height="88rpx" leftIconColor="#fff"
			titleStyle='color:#fff;font-size:32rpx' left-icon-size="32rpx" bg-color="#FF594D" :autoBack="true">
		</u-navbar>

		<view class="tab">
			<u-tabs :activeStyle='activeColor' :current='current' lineWidth='170rpx' :inactiveStyle="inactiveStyle33"
				lineHeight="4rpx" lineColor="#FF594D" :itemStyle="itemStyle" :list="list1" @click="tabClick"></u-tabs>
		</view>
		<view class="order_content" v-if="currentOrders.length!=0">
			<template v-for="(item,index) in currentOrders">
				<view class="order_item" v-if="item" :key="index">
					<view class="" @click="navigateTo('/pages_mine/pages/order-detail/order-detail?id='+item.id)">
						<view class="order_status">
							<view class="order_num">
								订单编号：{{item.number}}
							</view>
							<view class="status">
								{{getStatusText(item.status)}}
							</view>
						</view>
						<view class="line">
							<u-line></u-line>
						</view>
						<view class="goods_list">
							<template v-for="(goods,goodsIndex) in item.orderDetails">
								<view class="goods_item">
									<goods-num :item='goods' :key="goodsIndex"></goods-num>
								</view>
							</template>
						</view>
						<view class="line">
							<u-line></u-line>
						</view>
					</view>
					<view class="operation">
						<view class="operation_money">
							需付款：<text>￥{{item.amount.toFixed(2)}}</text>
						</view>
						<view class="operation_right_content">
							<view @click.stop="delModel_show = true;orderNumber=item.number" v-if="item.payStatus==0&&item.status!=6"
								class="btn operation_left">
								取消订单
							</view>
							<view @click="delModel_show = false;navigateTo('/pages_goods/user-payment/user-payment?orderId='+item.id)"
								v-if="item.payStatus==0 &&item.status!=6" class="btn operation_right">
								立即支付
							</view>
							<view v-if="item.status!=1 && item.status!=2" @click.stop='1' class="btn operation_left">
								{{getPayStatus(item.payStatus)}}
							</view>
							<view @click="receipt_show=true;order=item" v-if="item.status==4" class="btn operation_right">
								确认收货
							</view>
						</view>
					</view>
				</view>
			</template>
		</view>
		<u-popup :show="delModel_show" round="10" mode="center">
			<view class="delModel1">
				<view class="delTitle">
					<text>取消订单</text>
					<view class="close" @click="delModel_show = false">
						<image src="../../static/images/address-administration/图标@2x.png" mode=""></image>
					</view>
				</view>
				<view class="delText">
					是否取消订单
				</view>
				<view class="operation">
					<view @click="delModel_show = false" class="btn operation_left">
						取消
					</view>
					<view @click="cancel" class="btn operation_right">
						确定
					</view>
				</view>
			</view>
		</u-popup>
		<u-popup :show="receipt_show" round="10" mode="center">
			<view class="delModel1">
				<view class="delTitle">
					<text>确认收货</text>
					<view class="close" @click="receipt_show = false">
						<image src="../../static/images/address-administration/图标@2x.png" mode=""></image>
					</view>
				</view>
				<view class="delText">
					是否确认收货
				</view>
				<view class="operation">
					<view @click="receipt_show = false" class="btn operation_left">
						取消
					</view>
					<view @click="receipt" class="btn operation_right">
						确定
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		ordersList,
		cancelAnOrder,
		userReceipt
	} from '../../../api/mine.js'
	export default {
		onLoad(option) {
			this.current = option.index
			// console.log(this.current);
		},
		// onShow() {
		// 	this.$router.go(0)
		// },
		data() {
			return {
				titleStyle: {
					color: '#fff'
				},
				list1: [{
					name: '全部',
				}, {
					name: '待付款',
				}, {
					name: '待发货'
				}, {
					name: '待收货'
				}, {
					name: '待取货'
				}, {
					name: '待拼单'
				}, {
					name: '退款'
				}, {
					name: '待评价'
				}],
				itemStyle: {
					width: '170rpx',
					height: '80rpx',
					margin: '0',
					padding: '0',
				},
				activeColor: {
					color: '#D54939',
					'font-weight': '800',
					'font-size': '26rpx'
				},
				inactiveStyle33: {
					color: '#777777',
					'font-size': '26rpx',
				},
				delModel_show: false,
				current: 0,
				orders: [],
				currentOrders: [],
				orderNumber: 0,
				receipt_show: false,
				order: {}
			}
		},
		created() {
			this.getOrdersList()
		},
		methods: {
			// 用户取消未支付订单
			async userCancelAnOrder(number) {
				const res = await cancelAnOrder(number)
				uni.showToast({
					title: res.msg
				})
				this.getOrdersList()
			},
			tabClick(item) {
				this.changeOrder(item.index)
			},
			changeOrder(index) {
				switch (index) {
					case 0:
						this.currentOrders = this.orders
						break
					case 1:
						this.currentOrders = this.orders.filter(item => {
							if (item.status == 1 || item.status == 2) {
								return item
							}
						})
						// console.log(this.currentOrders)
						break
					case 2:
						this.currentOrders = this.orders.filter(item => {
							if (item.status == 3) return item
						})
						break
					case 3:
						this.currentOrders = this.orders.filter(item => {
							if (item.status == 4) return item
						})
						break
					case 4:
						this.currentOrders = this.orders.filter(item => {
							if (item.status == 10)
								return item
						})
						break
					case 5:
						this.currentOrders = this.orders.filter(item => {
							if (item.status == 9)
								return item
						})
						break
					case 6:
						this.currentOrders = this.orders.filter(item => {
							if (item.status == 7 || item.status == 8)
								return item
						})
						break
					case 7:
						this.currentOrders = this.orders.filter(item => {
							if (item.comments == 1 && item.status == 5)
								return item
						})
						break
				}
			},
			navigateTo(url) {
				uni.navigateTo({
					url
				})
			},
			// 获取订单列表
			async getOrdersList() {
				const res = await ordersList()
				console.log(res);
				this.orders = res.rows
				this.changeOrder(this.current - 0)
			},
			// 取消订单
			cancel() {
				this.userCancelAnOrder(this.orderNumber)
				this.delModel_show = false
			},
			// 确认收货
			receipt() {
				// console.log(item);
				this.confirmReceipt({
					number: this.order.number,
					buyNumber: this.order.buyNumber,
					orderType: this.order.orderType,
					mode: this.order.mode
				})
			},
			async confirmReceipt(data) {
				const res = await userReceipt(data)
				console.log(res);
				if (res.code != 200) {
					uni.showToast({
						title: res.msg,
						icon: 'error'
					})
					this.receipt_show = false
					return
				}
				uni.showToast({
					title: res.msg,
				})
				this.receipt_show = false
			},
			getStatusText(status) {
				switch (status) {
					// 订单状态 1待付款 2模糊支付状态 3 待发货 4已发货 5已完成 
					// 6已取消 7退款中 8管理员同意退款 9待拼单 10待取货
					case 1:
					case 2:
						return '待付款'
						break
					case 3:
						return '待发货'
						break
					case 4:
						return '已发货'
						break
					case 5:
						return '已完成'
						break
					case 6:
						return '已取消'
						break
					case 7:
						return '退款中'
						break
					case 8:
						return '管理员同意退款'
						break
					case 9:
						return '待拼单'
						break
					case 10:
						return '待取货'
						break
				}
			},
			getPayStatus(status) {
				switch (status) {
					// 订单状态 1待付款 2模糊支付状态 3 待发货 4已发货 5已完成 
					// 6已取消 7退款中 8管理员同意退款 9待拼单 10待取货
					case 0:
						return '未支付'
						break
					case 1:
						return '已支付'
						break
					case 2:
						return '已退款'
						break
				}
			}
		}
	}
</script>

<style lang="scss">
	.content {
		.u-safe-area-inset-bottom {
			padding: 0;
		}

		background-color: #f3f3f3;
		min-height: 100vh;
		// padding-bottom: 20rpx;

		// padding-top: 35rpx;
		.tab {
			background-color: #fff;
			margin-bottom: 20rpx;
		}

		.order_content {
			.order_item {
				padding: 25rpx 20rpx 24rpx 40rpx;
				background-color: #fff;
				margin-bottom: 20rpx;
				border-radius: 20rpx 20rpx 20rpx 20rpx;

				.goods_item {
					margin-bottom: 20rpx;

					&:last-of-type {
						margin-bottom: 0;
					}
				}

				.order_status {
					display: flex;
					justify-content: space-between;

					.order_num {
						font-size: 24rpx;
						font-family: PingFang SC, PingFang SC;
						font-weight: 500;
						color: #777777;

					}

					.status {
						font-size: 26rpx;
						font-family: PingFang SC, PingFang SC;
						font-weight: 800;
						color: #FF2525;
					}
				}

				.line {
					padding: 20rpx 0;
				}

				.operation {
					display: flex;
					justify-content: space-between;
					align-items: center;

					.operation_money {
						font-size: 24rpx;
						font-weight: 500;
						color: #777777;

						text {
							font-size: 32rpx;
							font-family: PingFang SC, PingFang SC;
							font-weight: 800;
							color: #FF2525;
						}
					}

					.operation_right_content {
						display: flex;
						justify-content: center;

						.btn {
							width: 170rpx;
							height: 56rpx;
							line-height: 56rpx;
							text-align: center;
							box-sizing: border-box;
							font-size: 26rpx;
							border-radius: 10rpx 10rpx 10rpx 10rpx;
						}

						.operation_left {
							border: 1rpx solid #FF594D;
							color: #FF594D;
						}

						.operation_right {
							margin-left: 37rpx;
							background: #FF594D;
							color: #FFFFFF;
						}
					}
				}
			}
		}

		.delModel1 {
			width: 656rpx;
			height: 339rpx;
			box-sizing: border-box;
			padding: 24rpx 20rpx;

			.delTitle {
				font-size: 32rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				color: #333332;
				display: flex;
				justify-content: space-between;

				image {
					width: 32rpx;
					height: 32rpx;
				}
			}

			.delText {
				height: 170rpx;
				text-align: center;
				line-height: 170rpx;
			}

			.operation {
				display: flex;
				justify-content: center;

				.btn {
					width: 170rpx;
					height: 56rpx;
					line-height: 56rpx;
					text-align: center;
					box-sizing: border-box;
					border-radius: 10rpx 10rpx 10rpx 10rpx;
				}

				.operation_left {
					border: 1rpx solid #FF594D;
					color: #FF594D;
					margin-right: 36rpx;
				}

				.operation_right {
					background: #FF594D;
					color: #FFFFFF;
				}
			}
		}
	}
</style>